package de.revdev.llt.bean.managed;

import de.revdev.llt.bean.entities.User;
import de.revdev.llt.bean.entities.interfaces.ClassRoomInterface;
import de.revdev.llt.bean.facade.UserFacade;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author f.lindermaier
 */
@Named(value = "userBean")
@SessionScoped
public class UserBean implements Serializable {

    private UserFacade uf;
    private HttpServletRequest request;
    private static final Logger log = Logger.getLogger(UserBean.class.getName());
    private User user;
    private String classRoomTemplate;
    private ClassRoomInterface classRoom;

    /**
     * Creates a new instance of UserBean
     */
    public UserBean() {
    }

    public User getUser() {
        if (this.user == null) {
            try {
                user = uf.findByEmail(request.getRemoteUser());
            } catch (Exception e) {
                log.log(Level.WARNING, "An User with username " + request.getRemoteUser() + " does not exist in the database", e);
            }
        }
        return user;
    }

    public void refreshUser() {
        uf.find(user.getId());
    }

    @Inject
    public void setUserFacade(UserFacade userFacade) {
        this.uf = userFacade;
    }

    @Inject
    public void setRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getClassRoomTemplate() {
        return classRoomTemplate;
    }

    public void setClassRoomTemplate(String classRoomTemplate) {
        this.classRoomTemplate = classRoomTemplate;
    }

    public ClassRoomInterface getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(ClassRoomInterface classRoom) {
        this.classRoom = classRoom;
    }
}
